package view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.IOException;
import java.io.OutputStream;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

public class CommunicationArea extends JPanel{

	private JTextArea log, server;
	private TextAreaOutputStream outputLog, outputServer;
	
	public CommunicationArea(){
		
		setLayout(new GridBagLayout());
		
		log = new JTextArea();
		log.setRows(5);
		JScrollPane scrolLog = new JScrollPane(log);
		JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.add("log", scrolLog);
		server = new JTextArea();
		server.setRows(5);
		JScrollPane scrollPane = new JScrollPane(server);
		JTabbedPane tabbedPane1 = new JTabbedPane();
		tabbedPane1.add("Server log", scrollPane);

		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,tabbedPane, tabbedPane1);
		splitPane.setOneTouchExpandable(true);
		splitPane.setContinuousLayout(true);
		splitPane.setDividerLocation(350);
		splitPane.setResizeWeight(1);
		add(splitPane,new GridBagConstraints(0,0,1,1,1.0,1.0,GridBagConstraints.PAGE_START,GridBagConstraints.BOTH,new Insets(0,0,5,5),0,0));
	}
	
	public void clearLog(){
		log.setText("");
	}

	/**
	 * This method set an OutputStream where take print to append to log 
	 * @param textAreaOutputStream
	 */
	public void setOutputStream(TextAreaOutputStream log, TextAreaOutputStream server){
		outputLog=log;
		outputServer=server;
		outputLog.setTextArea(this.log);
		outputServer.setTextArea(this.server);
	}
	
	/**
	 * Nested class, this class automatically append an print from OutputStream
	 *
	 */
	public static class TextAreaOutputStream extends OutputStream {
	    private JTextArea textControl;
	    
	    /**
	     * Creates a new instance of TextAreaOutputStream.
	     * The javax.swing.JTextArea will be must set with setTextArea method.
	     *
	     */
	    public TextAreaOutputStream() {
	  
	    }
	    
	    /**
	     * Creates a new instance of TextAreaOutputStream which writes
	     * to the specified instance of javax.swing.JTextArea control.
	     *
	     * @param control   A reference to the javax.swing.JTextArea
	     *                  control to which the output must be redirected
	     *                  to.
	     */
	    public TextAreaOutputStream( JTextArea control ) {
	        textControl = control;
	    }
	    
	    public void setTextArea(JTextArea textArea){
	    	textControl=textArea;
	    }
	    
	    /**
	     * Writes the specified byte as a character to the 
	     * javax.swing.JTextArea.
	     *
	     * @param   b   The byte to be written as character to the 
	     *              JTextArea.
	     */
	    public void write( int b ) throws IOException {
	        // append the data as characters to the JTextArea control
	    	if(textControl!=null)
	    		textControl.append( String.valueOf( ( char )b ) );
	    	else
	    		System.out.print( String.valueOf( ( char )b ) );
	    }
	    
	}
}
